package com.turnengine.client.local.group.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.group.bean.IGroup;
import com.turnengine.client.local.group.command.AddChildGroup;
import com.turnengine.client.local.group.command.AddParentGroup;
import com.turnengine.client.local.group.command.GetGroupById;
import com.turnengine.client.local.group.command.GetGroupByName;
import com.turnengine.client.local.group.command.GetGroupListByFactionName;
import com.turnengine.client.local.group.command.GetGroups;
import com.turnengine.client.local.group.command.IAddChildGroup;
import com.turnengine.client.local.group.command.IAddParentGroup;
import com.turnengine.client.local.group.command.IGetGroupById;
import com.turnengine.client.local.group.command.IGetGroupByName;
import com.turnengine.client.local.group.command.IGetGroupListByFactionName;
import com.turnengine.client.local.group.command.IGetGroups;
import java.util.List;

/**
 * The Local Group Command Set.
 */
public class LocalGroupCommandSet implements ILocalGroupCommandSet {

	@Override
	public IGroup addParentGroup(long loginId, int instanceId, String name, String factionName) {
		IAddParentGroup command = new AddParentGroup(loginId, instanceId, name, factionName);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IGroup> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IGroup addChildGroup(long loginId, int instanceId, String name, String parentName, String factionName) {
		IAddChildGroup command = new AddChildGroup(loginId, instanceId, name, parentName, factionName);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IGroup> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IGroup getGroupById(long loginId, int instanceId, int id) {
		IGetGroupById command = new GetGroupById(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IGroup> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IGroup getGroupByName(long loginId, int instanceId, String name) {
		IGetGroupByName command = new GetGroupByName(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IGroup> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IGroup> getGroups(long loginId, int instanceId) {
		IGetGroups command = new GetGroups(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IGroup>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IGroup> getGroupListByFactionName(long loginId, int instanceId, String name) {
		IGetGroupListByFactionName command = new GetGroupListByFactionName(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IGroup>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
